package com.requiem.hist_group_manage_springboot.service.impl;

import com.requiem.hist_group_manage_springboot.common.HttpParamUtil;
import com.requiem.hist_group_manage_springboot.common.LogUtil;
import com.requiem.hist_group_manage_springboot.common.PageUtil;
import com.requiem.hist_group_manage_springboot.constants.SystemConstants;
import com.requiem.hist_group_manage_springboot.exception.R;
import com.requiem.hist_group_manage_springboot.mapper.GroupLogMapper;
import com.requiem.hist_group_manage_springboot.mapper.GroupMapper;
import com.requiem.hist_group_manage_springboot.model.GroupLogModel;
import com.requiem.hist_group_manage_springboot.model.GroupModel;
import com.requiem.hist_group_manage_springboot.model.StudentModel;
import com.requiem.hist_group_manage_springboot.model.TeacherModel;
import com.requiem.hist_group_manage_springboot.service.GroupService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class GroupServiceImpl implements GroupService {


    @Autowired
    GroupMapper groupMapper;
    @Autowired
    GroupLogMapper groupLogMapper;
    @Autowired
    HttpParamUtil httpParamUtil;
    @Autowired
    LogUtil logUtil;
    @Autowired
    PageUtil pageUtil;

    @Override
    public R getList() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        Map<String, Object> result = pageUtil.executePageQuery(params, () -> groupMapper.getGroupList());
        return R.success(result);
    }

    @Override
    public R getByID() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        int id = Integer.parseInt(params.get("id"));

        GroupModel group = groupMapper.getGroupByID(id);
        return R.success(group);
    }

    @Override
    public R getStudent() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        int id = Integer.parseInt(params.get("id"));
        Map<String, Object> result = pageUtil.executePageQuery(params, () -> groupMapper.getStudent(id));
        return R.success(result);
    }

    @Override
    public R getTeacher() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        int id = Integer.parseInt(params.get("id"));
        Map<String, Object> result = pageUtil.executePageQuery(params, () -> groupMapper.getTeacher(id));
        return R.success(result);
    }

    @Override
    public R getLog() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        int id = Integer.parseInt(params.get("id"));
        List<GroupLogModel> log = groupLogMapper.getLog(null, null, id);
        for (GroupLogModel groupLogModel : log) {
            String logStr = groupLogModel.getLog();
            String simpleLog = logStr.replaceAll("在.*?时", "");
            simpleLog = simpleLog.replaceAll(".*学生", "学生");
            groupLogModel.setLog(simpleLog);
        }
        return R.success(log);
    }


    @Override
    public R add(@NonNull GroupModel groupModel) {
        GroupModel groupExit = groupMapper.getGroupExit(groupModel.getName());
        if (groupExit != null) {
            return R.error(SystemConstants.DATA_DUPLICATE, SystemConstants.DATA_DUPLICATE_MSG);
        }
        if (groupModel.getCreateTime() == null) {
            groupModel.setCreateTime(LocalDateTime.now());
        }
        int i = groupMapper.addGroup(groupModel);
        if (i < 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        return R.success();
    }

    @Override
    public R delete() {
        Map<String, String> params = httpParamUtil.parseQueryString();
        int id = Integer.parseInt(params.get("id"));
        GroupModel group = groupMapper.getGroupByID(id);
        if (group == null) {
            return R.error(SystemConstants.DATA_DUPLICATE, SystemConstants.DATA_DUPLICATE_MSG);
        }
        List<StudentModel> student = groupMapper.getStudent(id);
        if (student != null) {
            for (StudentModel studentModel : student) {
                logUtil.recordStudent(studentModel.getId(), id, SystemConstants.EXIT_GROUP);
            }
        }
        List<TeacherModel> teacher = groupMapper.getTeacher(id);
        if (teacher != null) {
            for (TeacherModel teacherModel : teacher) {
                logUtil.recordTeacher(teacherModel.getId(), id, SystemConstants.EXIT_GROUP);
            }
        }
        int i = groupMapper.deleteGroup(id);
        if (i < 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        return R.success();
    }

    @Override
    public R edit(@NonNull GroupModel groupModel) {
        GroupModel group = groupMapper.getGroupByID(groupModel.getId());
        if (group == null) {
            return R.error(SystemConstants.DATA_DUPLICATE, SystemConstants.DATA_DUPLICATE_MSG);
        }
        int i = groupMapper.editGroup(groupModel);
        if (i < 1) {
            return R.error(SystemConstants.ERROR_CODE, SystemConstants.ERROR_MSG);
        }
        return R.success();
    }

    @Override
    public R SearchList(GroupModel groupModel) {
        Map<String, String> params = httpParamUtil.parseQueryString();
        Map<String, Object> result = pageUtil.executePageQuery(params, () -> groupMapper.getSearchList(groupModel));
        return R.success(result);
    }

}
